package com.frxuexi.visit.view

import android.util.Log
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.adapter.VisitAdapter
import com.frxuexi.visit.adapter.VisitStatusAdapter
import com.frxuexi.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitIntent, VisitViewModel>() {
    override val binding: ActivityVisitBinding by lazy { ActivityVisitBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }
    var con=""
    var position=0
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<VisitBeanItem>
                                visitAdapter.apply {
                                    data.clear()
                                    addData(items)
                                    notifyDataSetChanged()
                                }
                                if(items.isNullOrEmpty()){
                                    vm.sendIntent(VisitIntent.getVisitByPurpose(con))
                                }
                            }
                            RequestType.DELETE->{
                                ToastUtils.showLong("删除成功")
                            }

                            else -> {}
                        }
                    }

                    is UiState.onError -> {
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val statusList = mutableListOf<StatusBean>(
            StatusBean("全部状态", true),
            StatusBean("待审核", false),
            StatusBean("已通过", false),
            StatusBean("驳回", false),
            StatusBean("已完成", false)
        )
        visitStatusAdapter.addData(statusList)
        binding.visitStatusRv.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity, RecyclerView.HORIZONTAL, false)
        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            this.position=position
            visitStatusAdapter.data.forEach {
                it.isCheck = false
            }
            visitStatusAdapter.data.get(position).isCheck = true
            visitStatusAdapter.notifyDataSetChanged()

            if(position==0){
                vm.sendIntent(VisitIntent.getVisits)
            }else{
                vm.sendIntent(VisitIntent.getVisitByStatus(visitStatusAdapter.data.get(position).status))
            }
        }



        binding.visitRv.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/visit/VisitApproveActivity").withString("vid",visitAdapter.data.get(position).id.toString()).navigation()
        }

        visitAdapter.setOnItemLongClickListener { adapter, view, position ->
            vm.sendIntent(VisitIntent.deleteVisit(visitAdapter.data.get(position).id!!))
            visitAdapter.notifyItemRemoved(position)
            return@setOnItemLongClickListener true
        }

        binding.visitAdd.setOnClickListener {
            ARouter.getInstance().build("/visit/VisitAddActivity").navigation()
        }

        binding.visitSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                con=query!!
                vm.sendIntent(VisitIntent.getVisitByName(query!!))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText.isNullOrEmpty()){
                    if(position==0){
                        vm.sendIntent(VisitIntent.getVisits)
                    }else{
                        vm.sendIntent(
                            VisitIntent.getVisitByStatus(
                                visitStatusAdapter.data.get(
                                    position
                                ).status
                            )
                        )
                    }
                }
                return true
            }

        })


    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(VisitIntent.getVisits)
    }

}